package com.lsj.core.page;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * @Author:lin.sj
 * @Data:2021/5/24 20:23
 * @Desc: 返回前端的通用分页
 */
@Data
@Accessors(chain = true)
public class ResultPage<T> {
    //页码大小
    private long size;
    //总页数
    private long pages;
    //当前页
    private long current;
    //总数据量
    private long total;

    private List records;

    public ResultPage(IPage page) {
        this.size = page.getSize();
        this.current = page.getCurrent();
        this.total = page.getTotal();
        this.pages = page.getPages();
    }

    public static  <T> ResultPage<T> getResult(IPage page, Class<T> tClass){
        ResultPage resultPage = new ResultPage(page);
        List<T> dtos = Convert.toList(tClass, page.getRecords());
        resultPage.setRecords(dtos);
        return resultPage;
    }
}
